package com.team3.ecommerce.service;

import com.team3.ecommerce.domain.ShoppingCartItem;
import com.team3.ecommerce.domain.ShoppingCartItemExample;
import com.team3.ecommerce.domain.ShoppingCartItemKey;
import com.team3.ecommerce.mapper.ShoppingCartItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;


@Service
public class ShoppingCartServiceImp implements ShoppingCartService {

    @Autowired
    private ShoppingCartItemMapper shoppingCartItemMapper;

    @Override
    public List<ShoppingCartItem> getUserShoppingCart(String user_uid) {
        ShoppingCartItemExample example = new ShoppingCartItemExample();
        example.or().andUserUidEqualTo(user_uid);
        return shoppingCartItemMapper.selectByExample(example);
    }

    @Override
    public void updateUserShoppingCart(String user_uid, String comm_uid, int comm_quantity,String item_checked) {

//        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");

        ShoppingCartItemExample example = new ShoppingCartItemExample();
        example.or().andUserUidEqualTo(user_uid).andCommUidEqualTo(comm_uid);
//        System.out.println("后端数据库开始处理1，现在是"+formatter.format(System.currentTimeMillis()));
        ShoppingCartItem item = shoppingCartItemMapper.selectByExample(example).get(0);
//        System.out.println("后端数据库处理完毕1，现在是"+formatter.format(System.currentTimeMillis()));
        if (item != null) {
            item.setCommQuantity(comm_quantity);
            item.setItemChecked(item_checked);
//            System.out.println("后端数据库开始处理2，现在是"+formatter.format(System.currentTimeMillis()));
            shoppingCartItemMapper.updateByExampleSelective(item, example);
//            System.out.println("后端数据库处理完毕2，现在是"+formatter.format(System.currentTimeMillis()));
        } else {
            ShoppingCartItem newItem = new ShoppingCartItem();
            newItem.setCommQuantity(comm_quantity);
            newItem.setUserUid(user_uid);
            newItem.setCommUid(comm_uid);
            newItem.setItemChecked("true");
//            System.out.println("后端数据库开始处理2，现在是"+formatter.format(System.currentTimeMillis()));
            shoppingCartItemMapper.insert(newItem);
//            System.out.println("后端数据库处理完毕2，现在是"+formatter.format(System.currentTimeMillis()));
        }
        return;
    }

    @Override
    public void addUserShoppingCartItem(String user_uid, String comm_uid) {
        ShoppingCartItemKey shoppingCartItemKey = new ShoppingCartItemKey(user_uid, comm_uid);
        ShoppingCartItem shoppingCartItem = shoppingCartItemMapper.selectByPrimaryKey(shoppingCartItemKey);
        if (shoppingCartItem != null) {
            shoppingCartItem.setCommQuantity(shoppingCartItem.getCommQuantity() + 1);
            shoppingCartItemMapper.updateByPrimaryKey(shoppingCartItem);
        } else {
            ShoppingCartItem shoppingCartItem1 = new ShoppingCartItem(user_uid,comm_uid,1,"true");
            shoppingCartItemMapper.insert(shoppingCartItem1);
        }
    }

    @Override
    public void deleteUserShoppingCartItem(String user_uid, String comm_uid) {
        ShoppingCartItemKey shoppingCartItemKey = new ShoppingCartItemKey(user_uid, comm_uid);
        shoppingCartItemMapper.deleteByPrimaryKey(shoppingCartItemKey);
        return;
    }

    @Override
    public void cleanUserShoppingCart(String user_uid) {
        ShoppingCartItemExample example = new ShoppingCartItemExample();
        example.or().andUserUidEqualTo(user_uid).andItemCheckedEqualTo("true");
        shoppingCartItemMapper.deleteByExample(example);
    }
}
